import { Collision, Description, Feature, Glyph, Impassable, Opaque, type Entity } from '$lib/components';
import { Ground } from '$lib/content/ground';
import { Types, defineComponent, type IWorld, removeComponent, hasComponent } from '$lib/ecs';

export const Diggable = defineComponent('Diggable', {});
export const Passage = defineComponent('Passage', {});
export const Water = defineComponent('Water', {});
export const NeedSwim = defineComponent('NeedSwim', {});

export const Coal = defineComponent('Coal', {
  amount: Types.ui32,
});

export const Gold = defineComponent('Gold', {
  amount: Types.ui32,
});

export const Foliage = defineComponent('Foliage', {
  amount: Types.ui32,
});

export const mowGrass = (world: IWorld, e: Entity, amount: number) => {
  if (!hasComponent(world, Foliage, e))
    return 0;
  if (Foliage.amount[e] <= amount) {
    const res = Foliage.amount[e];
    removeComponent(world, Foliage, e);
    return res;
  }
  Foliage.amount[e] -= amount;
  return amount;
};

export const groundTemplate = [
  [Ground],
  [Glyph, {
    glyph: '#',
    color: '#1116',
  }],
  [Description, {
    summary: "soil",
    description: "It's a normal soil. You feel cozy here and comfortable digging through it.",
  }],
  [Opaque],
  [Collision],
  [Diggable],
  [Coal],
  [Gold],
];

export const surfaceGroundTemplate = [
  [Ground],
  [Glyph, {
    glyph: '.',
    color: '#2008',
    background: '#631',
  }],
  [Description, {
    summary: "ground",
    description: "Good old soil. Rich in nutrients for plants.",
  }],
  [Foliage],
];

export const sandTemplate = [
  [Ground],
  [Glyph, {
    glyph: '.',
    color: '#b82',
    background: '#971',
  }],
  [Description, {
    summary: "sand",
    description: "Sand. Common thing along the shore. Not very fun.",
  }],
];

export const oceanTemplate = [
  [Ground],
  [Glyph, {
    glyph: '~',
    color: '#13b',
    background: '#129',
  }],
  [Description, {
    summary: "ocean",
    description: "Big pool of salty water. Very big. You don't know what lies beyond if anything. For all you know it can stretch forever.",
  }],
  [Water],
  [NeedSwim],
];

export const shallowWaterTemplate = [
  [Ground],
  [Glyph, {
    glyph: '~',
    color: '#35d',
    background: '#14b',
  }],
  [Description, {
    summary: "shallow water",
    description: "Water is the source of life, but you don't like being wet that much. Still you can tread through this shallow waters if needed.",
  }],
  [Water],
];
